//
//  YSHomeGridItemView.m
//  Yueshi
//
//  Created by Northwan on 15/7/20.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSHomeGridItemView.h"
@interface YSHomeGridItemView()
@property (strong, nonatomic)UIImageView *imageView1;
@property (strong, nonatomic)UIImageView *imageView2;
@property (strong, nonatomic)UILabel *descLabel1;
@property (strong, nonatomic)UILabel *descLabel2;
@end

static const NSString *kHomeGridItemAdvertObj = @"kHomeGridItemAdvertObj";

@implementation YSHomeGridItemView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tapLeft =  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapItemLeft:)];
        UITapGestureRecognizer *tapRight =  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapItemRight:)];
        
        
        //商品图片1
        UIImageView *imageView1 = [[UIImageView alloc]init];
        [self addSubview:imageView1];
        imageView1.userInteractionEnabled = YES;
        self.imageView1 = imageView1;
        
        //描述1
        UILabel *descLabel1 = [[UILabel alloc] init];
        descLabel1.textColor = DescColor;
        descLabel1.font = SYS_FONT(DescFont);
        descLabel1.numberOfLines = 2;
        [self addSubview:descLabel1];
         descLabel1.userInteractionEnabled = YES;
        [descLabel1 addGestureRecognizer:tapLeft];
        self.descLabel1 = descLabel1;
        
        //商品图片2
        UIImageView *imageView2 = [[UIImageView alloc]init];
        [self addSubview:imageView2];
         imageView2.userInteractionEnabled = YES;
         self.imageView2 = imageView2;

        //描述2
        UILabel *descLabel2 = [[UILabel alloc] init];
        descLabel2.textColor = DescColor;
        descLabel2.font = SYS_FONT(DescFont);
        descLabel2.numberOfLines = 2;
        descLabel2.userInteractionEnabled = YES;
        [descLabel2 addGestureRecognizer:tapRight];
        [self addSubview:descLabel2];
        self.descLabel2 = descLabel2;
    }
    return self;
}


-(void)setCardGridFrame:(YSCardGridItemFrame *)cardGridFrame{
    _cardGridFrame = cardGridFrame;
    
   NSArray *adverts = cardGridFrame.adverts;
   YSAdvert *advert =  adverts[0];
   if(advert){
        [self.imageView1 setImageURLStr:advert.advertImg];
        self.imageView1.frame = cardGridFrame.imageFrame1;
         [self.imageView1 setCornerRadius:5.0 borderWidth:0 borderColor:[UIColor grayColor]];
       //添加遮盖,为做点击
       UIButton *cover = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.imageView1.width, self.imageView1.height)];
       [cover setBackgroundColor:[UIColor clearColor]];
       [cover addTarget:self action:@selector(tapCoverLeft:) forControlEvents:UIControlEventTouchUpInside];
       
       [self.imageView1 addSubview:cover];
       
        if(!ISBLANK(advert.advertText)){
            [self.descLabel1 setText:advert.advertText];
            self.descLabel1.frame = cardGridFrame.descFrame1;
        }
   }
    
   if(adverts.count == 2){
       YSAdvert *advert = adverts[1];
       [self.imageView2 setImageURLStr:advert.advertImg];
       self.imageView2.frame = cardGridFrame.imageFrame2;
       [self.imageView2 setCornerRadius:5.0 borderWidth:0 borderColor:[UIColor grayColor]];
        //添加遮盖,为做点击
       UIButton *cover = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.imageView2.width, self.imageView2.height)];
       [cover setBackgroundColor:[UIColor clearColor]];
       [cover addTarget:self action:@selector(tapCoverRight:) forControlEvents:UIControlEventTouchUpInside];
       
       [self.imageView2 addSubview:cover];
       
       if(!ISBLANK(advert.advertText)){
           [self.descLabel2 setText:advert.advertText];
           self.descLabel2.frame = cardGridFrame.descFrame2;
       }
   }
    
   self.cellHeight = cardGridFrame.cellHeight;
   self.frame = cardGridFrame.frame;
}

-(void)tapCoverLeft:(UIButton *)sender{
   NSLog(@"tapCoverLeft");
   YSAdvert *advert =  _cardGridFrame.adverts[0];
    if(_sourceType == HomeSourceType){
        [Utils dealUrlWithController:self.superview.superview.superview.superview.superview.viewController url:advert.advertUrl fromWebView:NO];
    }else{
        [Utils dealUrlWithController:self.viewController url:advert.advertUrl fromWebView:NO];
    }
}
-(void)tapCoverRight:(UIButton *)sender{
    NSLog(@"tapCoverRight");
    YSAdvert *advert =  _cardGridFrame.adverts[1];
    if(_sourceType == HomeSourceType){
        [Utils dealUrlWithController:self.superview.superview.superview.superview.superview.viewController url:advert.advertUrl fromWebView:NO];
    }else{
        [Utils dealUrlWithController:self.viewController url:advert.advertUrl fromWebView:NO];
    }
}

-(void)tapItemLeft:(UIGestureRecognizer *)recognizer{
    NSLog(@"tapItemLeft");
    YSAdvert *advert =  _cardGridFrame.adverts[0];
    if(_sourceType == HomeSourceType){
        [Utils dealUrlWithController:self.superview.superview.superview.superview.superview.viewController url:advert.advertUrl fromWebView:NO];
    }else{
        [Utils dealUrlWithController:self.viewController url:advert.advertUrl fromWebView:NO];
    }
}

-(void)tapItemRight:(UIGestureRecognizer *)recognizer{
    NSLog(@"tapItemRight");
    YSAdvert *advert =  _cardGridFrame.adverts[1];
    if(_sourceType == HomeSourceType){
        [Utils dealUrlWithController:self.superview.superview.superview.superview.superview.viewController url:advert.advertUrl fromWebView:NO];
    }else{
        [Utils dealUrlWithController:self.viewController url:advert.advertUrl fromWebView:NO];
    }
}

@end
